// Jair Cazarin <jair.cazarin@gmail.com>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Usage()
{
   printf("Usage: ShiftCipher.exe -[e|d] k \"string to be encrypted\"\n");
   printf("Where 0 <= k >= 25");
}

void Encrypt(char *str, int k)
{
   printf("%d", 'z'-'a');
   for(int i = 0; i < strlen(str); i++)
      printf("%c", ((((str[i]-'a') + k) % 26) + 'a'));
   printf("\n");
}

void Decrypt(char *str, int k)
{
   for(int i = 0; i < strlen(str); i++)
      printf("%c", ((((str[i]-'a') - k) % 26) + 'a'));
   printf("\n");
}

int main(int argc, char *argv[])
{
    if(argc < 4)
    {
       Usage();
       return 0;
    }
    int k = atoi(argv[2]);
    if(!(k>=0 && k<=25))
    {
        printf("K must be between 0 and 25 inclusive.\n");
        return 0;    
    }
    switch(argv[1][1])
    {
      case 'e': Encrypt(argv[3], k);
           break;
      case 'd': Decrypt(argv[3], k);
           break;
      default: Usage();
           break;
    }
    return 0;
}
